import 'dart:io';

import 'package:sinosheets/utils/storage/LSUtil.dart';

/// HTTP配置
class HttpConfig {
  /// 基础URL
  static String baseUrl = "";

  /// 超时时间配置
  static Duration connectTimeout = const Duration(seconds: 10);
  static Duration receiveTimeout = const Duration(seconds: 20);
  static Duration sendTimeout = const Duration(seconds: 10);

  /// 内容类型
  static const String contentType = "application/json; charset=utf-8";

  /// 接受类型
  static const String accept = "application/json,*/*";

  /// 平台
  static String get platform => Platform.isAndroid ? "android" : "ios";

  /// 重试配置
  static const int maxRetries = 3;
  static const Duration retryDelay = Duration(seconds: 1);

  /// 获取默认请求头
  static Map<String, String> getDefaultHeaders() {
    return {
      'Accept': accept,
      'Content-Type': contentType,
      'User-Agent': 'itaobuy-${platform == 'ios' ? 'iOS' : 'Android'}',
      'platform': platform == 'ios' ? 'iOS' : 'Android',
      // 'lang': LSUtil.getLanguage(),
      'lang': 'en',
      'version': LSUtil.getAPPVersion(),
      'device': 'app',
    };
  }

  /// 初始化配置
  static void init({
    required String baseUrl,
    Duration? connectTimeout,
    Duration? receiveTimeout,
    Duration? sendTimeout,
  }) {
    HttpConfig.baseUrl = baseUrl;
    if (connectTimeout != null) HttpConfig.connectTimeout = connectTimeout;
    if (receiveTimeout != null) HttpConfig.receiveTimeout = receiveTimeout;
    if (sendTimeout != null) HttpConfig.sendTimeout = sendTimeout;
  }
}

/// HTTP方法
enum HttpMethod {
  get(value: "GET"),
  post(value: "POST"),
  postFormData(value: "POST"),
  delete(value: "DELETE"),
  put(value: "PUT");

  const HttpMethod({
    required this.value,
  });

  final String value;
}
